$(window).on("load", function () {
    $('#preloader').fadeOut('slow', function () {
        $(this).remove();
    });
});

function Mypop(btnOpts) {
    var initBtnOpts = {
        callback: function () {} //确定点击后的事件
    };
    btnOpts = $.extend(true, initBtnOpts, btnOpts);
    $node = btnOpts.node;
    $node.off("click", ".close");
    $node.off("click", "[data-type=confirmBtnTxt]");
    $node.show();
    var ww = $(window).width();
    var popleft = ww / 2 - 300;
    if (ww > 750) {
        $node.find(".pop_box").css({
            width: '600px',
            left: popleft + 'px'
        });
    }
    //ww > 750 ? (ww = 750) : (ww = ww);

    $node.find(".pop_box").addClass("animation-dialogue-in");

    /* $("body,html").addClass("forbid-scroll"); */
    $node.on("click", ".close", function () {
        $(this)
            .parents(".my_pop")
            .hide();
        $node.find(".pop_box").removeClass("animation-dialogue-in");
        $node.find(".el-form-item__error").text('');

        /* $("body,html").removeClass("forbid-scroll"); */
    });

    $node.on("click", "[data-type=confirmBtnTxt]", function () {
        if (btnOpts.hasOwnProperty('callback')) {
            btnOpts.callback();
        }

    });
    if (btnOpts.hasOwnProperty('content')) {
        $node.find('.pop_body').html(btnOpts.content);
    }

}
Mypop.prototype.hide = function (node) {
    node.hide();
    node.removeClass("animation-dialogue-in");
}
//消失提示框
function Mytip(html) {
    var timer=null;
   
	$(".my-msg").off("click", ".close");
	var modalDiv =' <div class="my-msg">\
    <button type="button" class="msg-close">×</button>\
    <p class="my-msg-content">'+html+'</p>\
 </div>'
	if($(".my-msg").length>0) return false;
	$("body").append(modalDiv);

	$(".my-msg").addClass("animation-dialogue-in-top"); //弹出层后，禁用body和html滚动
	/* $("body,html").addClass("forbid-scroll"); */
	$(".my-msg").on("click", ".msg-close", function () {
		$(this)
			.parents(".my-msg")
			.remove();
            clearTimeout(timer);
	});
    timer = setTimeout(function () {
		$('.my-msg').fadeOut('slow', function () {
			$(this).remove();
            clearTimeout(timer);
		});
	}, 5000);
}
//验证邮箱
function IsEmail(str) {
    var reg = /^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/
    return reg.test(str);
}
$(function () {
    var objLang = null; //语言
    setPos();
    initSlectedLang();

    $(window).resize(function () {
        setPos()
    });

    //登录验证

    $("#login-btn").click(function () {

        var isCommit = true;
        $(".fbit-form").find("input[type=text]").each(function () {
            if ($(this).val() == '') {
                var phold = $(this).attr('placeholder');
                isCommit = false;
                $(".fbit-error").text(objLang['error'] + ' ' + phold);
                return false
            }
        })
        if (isCommit) {

            $(".fbit-error").text('');
            var data = $(".form").serialize();
           
            $.ajax({
                type: "GET",
                url: "",
                data: data,
                dataType: "json",
                success: function (res) {
                    console.log(res);
                    Mytip('提交成功');
                },
                error: function (xhr, error) {
                    Mytip(error);
                }
            })

        }

    });


    //点击注册弹窗
    $('.fbit-register a').click(function () {
        var pop = new Mypop({
            node: $('.my_pop_form'),
            callback: function () {
                //注册验证
                var isCommit = true;
                $('.pop_form .required-self').find('input').each(function () {
                    if ($(this).val() == '') {
                        var tit = $(this).closest('.el-form-item').find('label').text();
                        isCommit = false;
                        $(".el-form-item__error").text(objLang['error'] + ' ' + tit);

                        return false
                    }
                    if ($(this).attr('name') == 'pop-email' && $(this).val() !== '') {
                        if (!IsEmail($(this).val())) {
                            $(".el-form-item__error").text(objLang['pop-email-err']);
                            isCommit = false;
                            return false;
                        }

                    }
                    if ($(this).attr('name') == 'pop-vpassword' && $(this).val() != '') {

                        if ($(this).val() != $('input[name=pop-password]').val()) {
                            $(".el-form-item__error").text(objLang['pop-pwd-err']);
                            isCommit = false;
                            return false;
                        }

                    }


                })
                if (isCommit) {
                    $(".el-form-item__error").text('');
                    var data = $(".pop_form").serialize();                  
                    pop.hide($('.my_pop_form'));
                    
                    $.ajax({
                        type: "GET",
                        url: "",
                        data: data,
                        dataType: "json",
                        success: function (res) {
                            console.log(res);
                            Mytip('已经提交')
                        },
                        error: function (xhr, error) {
                            console.log(error);
                            Mytip(error)
                        }
                    })
                   

                }

            }
        });
    })

    //点击修改语言 并存入localstorage
    $('select[name=lang]').change(function () {
        var langv = $(this).val();
        if (localStorage.getItem("lang") === langv) {
            return false;
        }
        localStorage.setItem('lang', langv);

        loadLang(langv);
    })

    // 手机和电脑样式切换
    function setPos() {
        if ($(window).width() <= 768) {
            $('.fbit-login-desc').hide();
            $('.fbit-mob').show();
        } else {
            $('.fbit-login-desc').show();
            $('.fbit-mob').hide();
        }
        // if ($(document.body).height() < $(window).height()) {
        //     $('.fbit-bg-wrap').css('height', $(window).height() + 'px');
        // }
    }

    //根据不同语言加载不同图片
    function loadLang(lang) {
        if (lang == 'zh-cn') {
            objLang = cnLang;
        } else if (lang == 'zh-tw') {
            objLang = twLang;
        } else {
            objLang = enLang;
        }
        $('title').html(objLang['head-title']);
        $('[data-lang=i18n-img]').each(function () {
            var title = $(this).attr('data-title');
            if (objLang.hasOwnProperty(title)) {
                $(this).attr('src', objLang[title]);
            }
        })
        $('[data-lang=i18n-txt]').each(function () {
            var title = $(this).attr('data-title');
            if (objLang.hasOwnProperty(title)) {
                $(this).html(objLang[title]);
            }
        })
        //placeholder多语言
        $('[data-lang=i18n-phold]').each(function () {
            var title = $(this).attr('data-title');
            if (objLang.hasOwnProperty(title)) {
                $(this).attr('placeholder', objLang[title])
            }

        })
        $('[data-lang=i18n-inputbtn]').each(function () {
            var title = $(this).attr('data-title');
            if (objLang.hasOwnProperty(title)) {
                $(this).val(objLang[title]);
            }

        })
    }
    //设置默认浏览器语言
    function initSlectedLang() {
        var lang = getLang();
        $('select[name=lang]').val(lang);
        loadLang(lang);
    }
    //获取浏览器语言
    function getLang() {

        var lang = '';
        if (localStorage.getItem("lang") != null) {
            lang = localStorage.lang
        } else {
            lang = (navigator.language || navigator.userLanguage).toLowerCase(); //常规浏览器语言和IE浏览器
            switch (lang) {
                case 'zh-tw':
                    lang = 'zh-tw';
                    break;
                case 'zh-cn':
                    lang = 'zh-cn';
                    break;
                default:
                    lang = 'en';
            }
        }
        return lang
    }


})